import {
  USER_LOGIN,
  INIT_USERINFO
} from './mutations-type'
export default {
  state: {
    //登录状态
    loginStatus: false,
    //token
    token: null,
    //用户信息 
    userInfo: {}
  },
  getters: {

  },
  mutations: {
    //存储个人信息
    [USER_LOGIN](state, user) {
      state.loginStatus = true
      state.token = user.token
      state.userInfo = user
      //持久化存储
      localStorage.setItem('teaUserInfo', JSON.stringify(user))
    },
    //读取个人信息
    [INIT_USERINFO](state) {
      let userInfo = JSON.parse(localStorage.getItem('teaUserInfo'))
      if (userInfo) {
        state.loginStatus = true
        state.token = userInfo.token
        state.userInfo = userInfo
      }
    },
    //退出登录
    loginOut(state){
        let userInfo = JSON.parse(localStorage.getItem('teaUserInfo'))
        if (userInfo) {
          state.loginStatus = false
          state.token = null
          state.userInfo = {}
          localStorage.removeItem('teaUserInfo')
        }
    }
  },
  actions: {

  }
}
